<?php 
/**
 * Memcache 单例类
 */
class Mem{

	private static $_instance;
	private static $_connect_type = '';
	private $_memcached;


	/**
	 * [__construct 私有构造器]
	 */
	private function __construct(){
		if(!class_exists('Memcache')){
			throw new Exception("Class Memcache not exists");
		}
		$func = self::$_connect_type;
		$this->_memcached = new Memcache();
		$this->_memcached->$func('localhost','11211');
	}

	/**
	 * [__clone 保证不被clone]
	 * @return [type] [description]
	 */
	private function __clone(){
		throw new Exception("Could not clone!!");
	}

	/**
	 * [getInstance 获得唯一实例]
	 * @param  string $type [connect | pconnect]
	 * @return [type]       [$_instance]
	 */
	public static function getInstance($type = 'connect'){
		self::$_connect_type = ($type == 'connect') ? $type : 'pconnect';
		if(!self::$_instance instanceof self){
			self::$_instance = new self();
		}
		return self::$_instance;
	}

	/**
	 * [set 存放数据到内存中]
	 * @param [type]  $key     [键名]
	 * @param [type]  $value   [值]
	 * @param string  $flag    [是否对值进行压缩(zlib) MEMCACHE_COMPRESSED]
	 * @param integer $timeout [存活时间]
	 */
	public function set($key,$value,$flag = 0 ,$timeout = 2592000){
		$this->_memcached->set($key,$value,$flag,$timeout);
	}

	/**
	 * [get 能过key 获取数据]
	 * @param  [type] $key [可以是数组]
	 * @return [type]      [description]
	 */
	public function get($key){
		return $this->_memcached->get($key);
	}

	/**
	 * [delete 删除一条数据]
	 * @param  [type] $key [description]
	 * @return [type]      [description]
	 */
	public function delete($key){
		return $this->_memcached->delete($key);
	}

	/**
	 * [get_server_status 获得服务器是否在线  0  否]
	 * @param  [type]  $host [服务器地址]
	 * @param  integer $port [端口号 默认 11211]
	 * @return [type]        [description]
	 */
	public function get_server_status($host,$port = 11211){
		return $this->_memcached->getServerStatus($host,$port);
	}


	/**
	 * [get_stats 获取服务器统计信息]
	 * @return [type] [description]
	 */
	public function get_stats(){
		return $this->_memcached->getStats();
	}

	/**
	 * [get_version 获取memcache版本号]
	 * @return [type] [description]
	 */
	public function get_version(){
		return 	$this->_memcached->getVersion();
	}

	/**
	 * [clean 清洗(删除)已存储的所有元素]
	 * @return [type] [description]
	 */
	public function clean(){
		return $this->_memcached->flush();
	}

	/**
	 * [close 关闭连接]
	 * @return [type] [description]
	 */
	public function close(){
		$this->_memcached->close();
	}





	






}
?>
